package Text5;
import java.util.*;
public class Main {
    static int N = 510,n,m,max=0x3f3f3f3f;
    public static int[][] g = new int[N][N];
    public static int[] d=new int[N];
    public static boolean[] st=new boolean[N];

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        Arrays.fill(d,max);
        d[1]=0;
        for (int i = 1; i <= n; i++) {
            Arrays.fill(g[i],max);
        }
        for (int i = 0; i < m; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            int c = scanner.nextInt();
            g[a][b]=g[b][a]=Math.min(g[a][b],c);
        }
        int t = prim();
        if(t == max) System.out.println("impossible");
        else System.out.println(t);
    }

    private static int prim() {
        int sum=0;
        for (int i = 0; i < n; i++) {
            int t=-1;
            for (int j = 1; j <=n ; j++) {
                if(!st[j]&&(t==-1||d[t]>d[j])){
                    t=j;
                }
            }
            if(d[t]==max){
                return max;
            }
            st[t]=true;
            sum+=d[t];
            for (int j = 1; j <=n ; j++) {
                if(!st[j]){
                    d[j]=Math.min(d[j],g[t][j]);
                }
            }
        }
        return sum;
    }
}
